package ssm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import ssm.domain.Personnel;
import ssm.mapper.PersonnelMapper;

import java.util.List;

/**
 * 用户控制
 */
@Controller
public class PersonnelController {

    @Autowired
    private PersonnelMapper personnelMapper;

    //用户列表
    @RequestMapping("/personnelList")
    public String list(Model model){
        List<Personnel> all = personnelMapper.findAll();
        model.addAttribute("pList",all);
        return "Personnel";
    }

    //跳转添加用户界面
    @RequestMapping("/toAddPersonnel")
    public String toAddPersonnel(){
        return "addPersonnel";
    }
    //添加用户
    @RequestMapping("/addPersonnel")
    public String addP(Personnel personnel){
        personnelMapper.add(personnel);
        return "forward:personnelList";
    }

    //删除
    @RequestMapping("delP")
    public String delete(Integer id){
        personnelMapper.delete(id);
        return "forward:personnelList";
    }

    //编辑
    @RequestMapping("/toEditP")
    public String toUpdate(Integer id,Model model){
        Personnel one = personnelMapper.findOne(id);
        model.addAttribute("oneP",one);
        return "editPersonnel";
    }
    @RequestMapping("/updateP")
    public String update(Personnel personnel){
        Integer id = personnel.getId();
        System.out.println(id);
        personnelMapper.update(personnel);
        return "forward:personnelList";
    }
}
